package data 
{
	import flash.utils.Dictionary;
	/**
	 * ...
	 * @author lee
	 */
	public class Chapter extends Object 
	{
		private static var _chapters:Array = [];
		private static var _lessons:Array = [];
		private static var _lessonDict:Dictionary = new Dictionary();
		private static var _index:int;
		public static var lessonCount:int;
		public var name:String;
		private var _items:Array;
		private var _length:int;
		private var _id:int;
		public function Chapter(xml:XML) 
		{
			_id = _index;
			_index++;
			_items = [];
			if (xml != null) {
				parse(xml);
				_chapters.push(this);
			}
		}
		public function getComplete():int {
			var c:int = 0;
			for (var i:int = 0; i < _length; i++) {
				var lesson:Lesson = getLesson(i);
				if (lesson.hasComplete()) {
					c++;
				}
			}
			return c;
		}
		public function get length():int {
			return _length;
		}
		
		public function get id():int 
		{
			return _id;
		}
		public function getLesson(index:int):Lesson {
			return _items[index];
		}
		public function getAiccData():String {
			var str:String="";
			for (var i:int = 0; i < _length; i++) {
				var lesson:Lesson = getLesson(i);
				str += lesson.getAiccData();
			}
			return str;
		}
		public function getScormData():String {
			var str:String="";
			for (var i:int = 0; i < _length; i++) {
				var lesson:Lesson = getLesson(i);
				str += lesson.getScormData()+",";
			}
			str = str.substr(0, -1);
			return str;
		}
		public function getLocalData():Array {
			var list:Array = [];
			for (var i:int = 0; i < _length; i++) {
				var lesson:Lesson = getLesson(i);
				list.push(lesson.getLocalData());
			}
			return list;
		}
		public function setLocalData(list:Array):void {
			if (list == null) return;
			for (var i:int = 0; i < _length; i++) {
				var lesson:Lesson = getLesson(i);
				lesson.setLocalData(list[i]);
			}
		}
		private function parse(xml:XML):void {
			name = xml.attribute("name");
			var childs:XMLList = xml.children();
			var len:int = childs.length();
			for (var i:int = 0; i < len; i++) {
				var lesson:Lesson = new Lesson();
				lesson.chapter = _id;
				lesson.lessonId = i;
				_lessonDict["id"+lesson.id] = lesson;
				_lessonDict["flag" + _id + ":" + i] = lesson;
				_lessonDict["index" + lesson.id] = i;
				lesson.parse(childs[i]);
				_items.push(lesson);
				_lessons.push(lesson);
			}
			_length = _items.length;
		}
		public static function getChapters():Array {
			return _chapters.concat();
		}
		public static function getChapter(index:int):Chapter {
			return _chapters[index];
		}
		public static function getLessons():Array {
			return _lessons.concat();
		}
		public static function getLessonById(id:int):Lesson {
			return _lessonDict["id" + id];
		}
		public static function getLessonByFlag(chapter:int, lesson:int):Lesson {
			return _lessonDict["flag" + chapter + ":" +lesson];
		}
		public static function getLessonIndex(id:int):int {
			return _lessonDict["index" + id];
		}
	}

}